package com.app.utils;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * 作者：lblbh on 2018/1/19 10:12
 * 邮箱：lanbuhan@163.com
 * 功能：Txt文件处理
 */

public class TxtUtils {

        public static void httpTxtContent(final String html, final OnFileStr onFileStr) {
            new Thread() {
                @Override
                public void run() {
                    super.run();

                    if(html.contains("http://")) {
                        org.apache.http.client.HttpClient client = new DefaultHttpClient();
                        HttpGet get = new HttpGet(html);
                        HttpParams httpParams = new BasicHttpParams();
                        HttpConnectionParams.setConnectionTimeout(httpParams, 8000);
                        HttpConnectionParams.setSoTimeout(httpParams, 8000);
                        get.setParams(httpParams);
                        // 发送请求
                        try {
                            HttpResponse response = client.execute(get);

                            if (response.getStatusLine().getStatusCode() == 200) {
                                InputStream loadFileData = response.getEntity().getContent();
                                if (null != loadFileData) {
                                    InputStreamReader inputReader = new InputStreamReader(loadFileData, "GB2312");
//                                    InputStreamReader inputReader = new InputStreamReader(loadFileData);
                                    if (null == inputReader)
                                        return;

                                    String str = parseSrt(inputReader);
                                    if (onFileStr != null) {
                                        onFileStr.onFileStr(str);
                                    }
                                }
                            }
                        } catch (ClientProtocolException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }.start();

        }

    /**
     * 解析txt内容
     */
    public static String parseSrt(InputStreamReader inputReader) {
        StringBuffer sb = new StringBuffer();
        //没有读到数据
        if(null == inputReader )
            return sb.toString();

        BufferedReader br = new BufferedReader(inputReader);
        String line = null;

        int key = 0;
        try {
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }} catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {
                inputReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return sb.toString();
    }
    public static interface OnFileStr{
        public void onFileStr(String result);
    }
}
